﻿/*
 * Copy Pasta
 * Created by Trevor Philip
 * 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * 
 * Class:       CfrmAddNewDbItem
 * Purpose:     Dialog box for adding new items to either the Forums, Categories, or Thread Types
 * 
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.OleDb;


namespace MyForumWatcher
{
    public partial class CfrmAddNewDbItem : Form
    {

        private int type;               //This refers to the type of item we are addin to the database
        private CfrmMainForm mainForm;
        private OleDbConnection conn;

        public CfrmAddNewDbItem(int _type, CfrmMainForm _mainForm)
        {
            InitializeComponent();

            mainForm = _mainForm;
            type = _type;

            conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database.mdb");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CfrmAddNew_Load(object sender, EventArgs e)
        {
            /*
             * Type 1 -> Entering a new forum
             * Type 2 -> Entering a new category
             * Type 3 -> Entering a new thread type
             * Type 4 -> Entering a new post
             * 
             */

            //Setting the label text accordingly
            if (type == 1)
                lblInstructions.Text = "Enter a name for the new forums ... ";
            if (type == 2)
                lblInstructions.Text = "Enter a name for the new category ... ";
            if (type == 3)
                lblInstructions.Text = "Enter a name for the new thread type ... ";
            if (type == 4)
                lblInstructions.Text = "Enter a name for the new post ... ";
            
        }

        private void AddNewForum()
        {

            //build new string
            StringBuilder query = new StringBuilder("");
            foreach (char ch in txtText.Text)
                if (ch == '\'')
                    query.Append("\'\'");
                else query.Append(ch);


            StringBuilder sql = new StringBuilder();
            sql.Append(" INSERT INTO Forums ");
            sql.Append(" VALUES ");
            sql.Append(" ('" + Guid.NewGuid().ToString() + "', '" + query.ToString() + "'); ");

            //------------------
            //Open the connection
            //------------------

            conn.Open();    

            OleDbCommand cmd = new OleDbCommand(sql.ToString(), conn);
            cmd.ExecuteNonQuery();

            mainForm.BSForums.EndEdit();
            mainForm.tableAdapterManager.ForumsTableAdapter.Update(mainForm.databaseDataSet.Forums);
            mainForm.forumsTableAdapter.Fill(mainForm.databaseDataSet.Forums);

            MessageBox.Show("New forums item added successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            mainForm.FillForumsData();

            //------------------
            //Close the connection
            //Dispose of the form
            //------------------
            conn.Close();
            //mainForm.FillData();
            this.Close();

        }

        private void AddNewCategory()
        {

            StringBuilder query = new StringBuilder("");
            foreach (char ch in mainForm.cmbForum.Text)
                if (ch == '\'')
                    query.Append("\'\'");
                else query.Append(ch);

            string sSQL = "SELECT GUID_Forums FROM Forums WHERE ForumsName = '" + query.ToString() + "'";

            //------------------
            //Open the connection
            //------------------

            conn.Open();

            OleDbCommand cmd = new OleDbCommand(sSQL, conn);

            OleDbDataReader reader = cmd.ExecuteReader();
            
            string GUID_Forums = "";

            while (reader.Read())
            {
                GUID_Forums = reader.GetString(0).ToString();
            }

            //------------------
            //Close the connection
            //------------------

            reader.Close();
            conn.Close();

            //build new string
            query = new StringBuilder("");
            foreach (char ch in txtText.Text)
                if (ch == '\'')
                    query.Append("\'\'");
                else query.Append(ch);

            StringBuilder sql = new StringBuilder();
            sql.Append(" INSERT INTO Categories ");
            sql.Append(" VALUES ");
            sql.Append(" ('" + Guid.NewGuid().ToString() + "', '" + GUID_Forums + "', '" + query.ToString() + "'); ");
            
            //------------------
            //Open the connection
            //------------------
            conn.Open();

            OleDbDataAdapter daForums = new OleDbDataAdapter(sql.ToString(), conn);
            cmd = new OleDbCommand(sql.ToString(), conn);
            cmd.ExecuteNonQuery();

            mainForm.BSCategories.EndEdit();
            mainForm.tableAdapterManager.CategoriesTableAdapter.Update(mainForm.databaseDataSet.Categories);
            mainForm.categoriesTableAdapter.Fill(mainForm.databaseDataSet.Categories);

            MessageBox.Show("New category added successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            mainForm.FillCategoriesData();

            try { mainForm.cmbCategory.SelectedItem = txtText.Text; } catch { }

            //------------------
            //Close the connection
            //Dispose of the form
            //------------------
            conn.Close();
            this.Close();
        }

        private void AddNewThreadType()
        {


            StringBuilder query = new StringBuilder("");
            foreach (char ch in mainForm.cmbCategory.Text)
            {
                if (ch == '\'')
                {
                    query.Append("\'\'");
                }
                else query.Append(ch);
            }

            string sSQL = "SELECT GUID_Category FROM Categories WHERE CategoryName = '" + query.ToString() + "'";


            //------------------
            //Open the connection
            //------------------
            conn.Open();

            OleDbCommand        cmd    = new OleDbCommand(sSQL, conn);
            OleDbDataReader     reader = cmd.ExecuteReader();

            string GUID_Category = "";

            while (reader.Read())
            {
                GUID_Category = reader[0].ToString();
            }

            //------------------
            //Close the connection
            //------------------
            conn.Close();
            reader.Close();

            query = new StringBuilder("");
            foreach (char ch in txtText.Text)
            {
                if (ch == '\'')
                {
                    query.Append("\'\'");
                }
                else query.Append(ch);
            }


            StringBuilder sql = new StringBuilder();
            sql.Append(" INSERT INTO ThreadType ");
            sql.Append(" VALUES ");
            sql.Append(" ('" + Guid.NewGuid().ToString() + "', '" + GUID_Category + "', '" + query.ToString() + "'); ");

            //------------------
            //Open the connection
            //------------------
            conn.Open();

            OleDbDataAdapter daForums = new OleDbDataAdapter(sql.ToString(), conn);
            cmd = new OleDbCommand(sql.ToString(), conn);
            cmd.ExecuteNonQuery();

            mainForm.BSThreadType.EndEdit();
            mainForm.tableAdapterManager.ThreadTypeTableAdapter.Update(mainForm.databaseDataSet.ThreadType);
            mainForm.threadTypeTableAdapter.Fill(mainForm.databaseDataSet.ThreadType);

            MessageBox.Show("New thread type added successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            mainForm.FillThreadTypeData();

            try { mainForm.cmbCategory.SelectedItem = txtText.Text; } catch { }

            //------------------
            //Close the connection
            //Dispose of the form
            //------------------

            conn.Close();
            this.Close();
        }

        private void AddNewPostName()
        {

            StringBuilder query = new StringBuilder("");
            foreach (char ch in mainForm.cmbThreadType.Text)
            {
                if (ch == '\'')
                {
                    query.Append("\'\'");
                }
                else query.Append(ch);
            }

            string sSQL = "SELECT GUID_ThreadType FROM ThreadType WHERE ThreadType = '" + query.ToString() + "'";

            //------------------
            //Open the connection
            //------------------
            conn.Open();

            OleDbCommand cmd = new OleDbCommand(sSQL, conn);
            OleDbDataReader reader = cmd.ExecuteReader();

            string GUID_ThreadType = "";

            while (reader.Read())
            {
                GUID_ThreadType = reader.GetString(0).ToString();
            }

            //------------------
            //Close the connection
            //------------------
            conn.Close();
            reader.Close();

            query = new StringBuilder("");
            foreach (char ch in txtText.Text)
            {
                if (ch == '\'')
                {
                    query.Append("\'\'");
                }
                else query.Append(ch);
            }

            StringBuilder sql = new StringBuilder();
            sql.Append(" INSERT INTO Post ");
            sql.Append(" VALUES ");
            sql.Append(" ('" + Guid.NewGuid().ToString() + "', '" + GUID_ThreadType + "', '" + query.ToString() + "', 'Answer goes here...'); ");

            //------------------
            //Open the connection
            //------------------
            conn.Open();

            OleDbDataAdapter daForums = new OleDbDataAdapter(sql.ToString(), conn);
            cmd = new OleDbCommand(sql.ToString(), conn);
            cmd.ExecuteNonQuery();

            mainForm.BSPost.EndEdit();
            mainForm.tableAdapterManager.PostTableAdapter.Update(mainForm.databaseDataSet.Post);
            mainForm.postTableAdapter.Fill(mainForm.databaseDataSet.Post);

            MessageBox.Show("New post added successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            mainForm.FillPostData();

            try { mainForm.cmbCategory.SelectedItem = txtText.Text; } catch { }

            //------------------
            //Close the connection
            //Dispose of the form
            //------------------
            conn.Close();
            this.Close();
        }

        private void btnOkay_Click(object sender, EventArgs e)
        {
            if (type == 1)
                AddNewForum();
            if (type == 2)
                AddNewCategory();
            if (type == 3)
                AddNewThreadType();
            if (type == 4)
                AddNewPostName();

            
        }

    }
}
